import pymysql
DBHOST='localhost'
DBUSER='root'
PASSWORD='kongxingli2002'
DBNAME='supermarket'
class Connect:
    #连接数据库
    def __init__(self):#self表示实例对象
        try:
            #连接数据库
            self.db=pymysql.connect(host=DBHOST,user=DBUSER,password=PASSWORD,database=DBNAME)
            print("数据库连接成功")
        except pymysql.Error as e:
            print('数据库连接失败'+str(e))

    #向表插入数据
    def insert(self,table):

        try:
            # 检查表是否存在
            cur=self.db.cursor()
            #sql插入语句
            sql='INSERT INTO 员工(员工编号,员工姓名,员工密码,员工级别,员工电话,员工工资,备注) VALUE(%s,%s,%s,%s,%s,%s,%s)'
            i = input('员工编号:')
            j = input('员工姓名:')
            k = input('员工密码:')
            l = input('员工级别:')
            n = input('员工电话:')
            m = input('员工工资:')
            o = input('备注:')
            #准备插入的值
            value = (i, j, k,l,n,m,o)
            cur.execute(sql, value)
            self.db.commit()
            print("表ok")
        except pymysql.Error as e:
            print('表 not ok' + str(e))
            self.db.rollback()
        self.db.close()

    #关闭数据库
    def __del__(self):
        if self.db:
            self.db.close()
            print("数据库连接已关闭")